<!--
 * @Author: Bennty
 * @Date: 2024-12-20 09:18:19
 * @LastEditors: 
 * @LastEditTime: 2024-12-27 10:18:58
 * @Description: 请填写简介
-->
<!--
 * @Author: Bennty
 * @Date: 2024-12-20 09:18:19
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-12-27 10:15:51
 * @Description: 请填写
-->
<template>
    <div class="p-2">
        <transition
            :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave"
        >
            <div class="search">
                <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="70px">
                    <el-form-item label="选择客户" prop="customerId">
                        <el-select v-model="queryParams.customerId" placeholder="请选择">
                            <el-option v-for="item in customerList" :key="item.id" :label="item.customerName" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否异常" prop="normal">
                        <el-select v-model="queryParams.normal" placeholder="请选择">
                            <el-option label="无异常" value="0"></el-option>
                            <el-option label="同金额收款多次" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="审核详情" prop="approvalStatus">
                        <el-select v-model="queryParams.approvalStatus" placeholder="请选择">
                            <el-option value="0" label="未审核"></el-option>
                            <el-option value="1" label="销售审核"></el-option>
                            <el-option value="2" label="销售审核中"></el-option>
                            <el-option value="3" label="财务审核中"></el-option>
                            <el-option value="4" label="财务审核完成"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="对账状态" prop="reconciliationStatus">
                        <el-select v-model="queryParams.reconciliationStatus" placeholder="请选择">
                            <el-option value="0" label="未对账"></el-option>
                            <el-option value="1" label="已对账"></el-option>
                            <el-option value="2" label="已回款"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="制单人" prop="createdByName">
                        <el-select v-model="queryParams.createdByName" placeholder="请选择">
                            <el-option v-for="user in roleKeyUserList" :key="user.userId" :label="user.userName" :value="user.userName"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <template #header>
                <el-row :gutter="8" class="mb8">
                    <el-col class="header-button-container" :span="1.5">
                        <el-button type="success" plain icon="Plus" @click="handleClickAdd" >新增</el-button>
                    </el-col>
                </el-row>
            </template>
            <el-table :data="orderList">
                <el-table-column label="制单日期" prop="createdByTime" align="center"></el-table-column>
                <el-table-column label="送货单号" prop="orderNumber" align="center"></el-table-column>
                <el-table-column label="制单人" prop="createdByName" align="center"></el-table-column>
                <el-table-column label="客户名称" align="center">
                    <template #default="scope">
                        {{ customerList.filter((item)=>item.id == scope.row.customerId)[0]?.customerName }}
                    </template>
                </el-table-column>
                <el-table-column label="当前所属公司" prop="company" align="center"></el-table-column>
                <el-table-column label="发货方式" align="center">
                    <template #default="scope">
                        {{ pay_type.filter((item: any)=>item.value == scope.row.sendMehod)[0]?.label }}
                    </template>
                </el-table-column>
                <el-table-column label="审核详情" align="center">
                    <template #default="scope">
                        <el-button link v-if="scope.row.approvalStatus == '0'" type="danger" @click="handleSalerExamine(scope.row)">未审核</el-button>
                        <el-button link v-else-if="scope.row.approvalStatus == '1'" type="primary" @click="handleSalerExamine(scope.row)">销售审核中</el-button>
                        <el-button v-else-if="scope.row.approvalStatus == '2'" link type="success" @click="handleClickFianceExamine(scope.row)">销售审核完成</el-button>
                        <span v-else-if="scope.row.approvalStatus == '3'"  @click="handleClickFianceExamine(scope.row)">财务审核中</span>
                        <span v-else-if="scope.row.approvalStatus == '4'" class="success">财务审核完成</span>
                    </template>
                </el-table-column>
                <el-table-column label="发货状态" align="center">
                    <template #default="scope">
                        <el-button link v-if="scope.row.deliverStatus == '0'" type="danger">未发货</el-button>
                        <el-button link v-else-if="scope.row.deliverStatus == '1'" type="success">可发货</el-button>
                        <el-button link v-else-if="scope.row.deliverStatus == '2'" type="primary">已发货</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="对账状态" align="center">
                    <template #default="scope">
                        <el-button link v-if="scope.row.reconciliationStatus == '0'" type="danger">未对账</el-button>
                        <span link v-else-if="scope.row.reconciliationStatus == '1'" class="purple" >已对账</span>
                        <span link v-else-if="scope.row.reconciliationStatus == '2'" class="warning" >已回款</span>
                    </template>
                </el-table-column>
                <el-table-column label="异常状态" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.normal == '0'" class="normal">无异常</span>
                        <span v-else-if="scope.row.normal == '1'" class="alert">同金额收款多次</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="180px">
                    <template #default="scope">
                        <el-tooltip content="打印" placement="top">
                            <el-button link :style="{fontSize:'15px'}" icon="Printer" type="primary" @click="handleClickPrint(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="编辑" placement="top">
                            <el-button link :style="{fontSize:'15px',color:'#B886F8'}" icon="Edit" @click="handleClickEdit(scope.row)" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button link :style="{fontSize:'15px'}" icon="Delete" type="danger" @click="handleClickDelete(scope.row)" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="撤销审核" placement="top">
                            <el-button link :style="{fontSize:'15px'}" icon="Back" type="warning" @click="handleRevokeExamine(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="查看付款凭证" placement="top">
                            <el-button link :style="{fontSize:'15px'}" icon="Money" type="primary" @click="handleClickCheckProof(scope.row)" ></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <addSaleOrder :visible="addOrderVisible" :send_type="send_type" @close="handleCloseAdd" />
        <printOrder :visible="printOrderVisible" @close="handleClosePrint"/>
        <editSalerOrder :visible="editOrderVisible" :send_type="send_type" @close="handleCloseEdit" />
        <salerExamine :visible="salerExamineVisible" :orderDetails="orderDetails" :payType="payType" :onlineMethod="online_payment_type" :offlineMehtod="offline_payment_type" @close="handleCloseSalerExamine" />
        <financeExamine :visible="fianceExamineVisible" :payType="payType" :onlineMethod="online_payment_type" :offlineMehtod="offline_payment_type" @close="handleCloseFianceExamine"/>
        <checkProof :visible="checkProofVisible" @close="handleCloseCheckProof" />
    </div>
</template>

<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import useCustomerStore from '@/store/modules/customers';
import useCRMStore from '@/store/modules/crm';
import addSaleOrder from './components/addSaleOrder.vue';
import printOrder from './components/printOrder.vue';
import editSalerOrder from './components/editSalerOrder.vue';
import salerExamine from './components/salerExamine.vue';
import financeExamine from './components/financeExamine.vue';
import checkProof from './components/checkProof.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pay_type,send_type,offline_payment_type,online_payment_type } = toRefs<any>(proxy?.useDict('pay_type', 'send_type','offline_payment_type','online_payment_type'));
const queryFormRef = ref<FormInstance>();
const { getCustomerData } = useCustomerStore()
const { customerList } = toRefs(useCustomerStore());
const { getUserListByRoleKey } = useCRMStore();
const { roleKeyUserList } = toRefs(useCRMStore());

const data = reactive({
    queryParams: {
        customerId: undefined,
        normal: undefined,
        approvalStatus: undefined,
        deliverStatus: undefined,
        reconciliationStatus: undefined,
        createdByName: undefined,
        orderNumber: undefined,
        pageNum: 1,
        pageSize: 30
    }
});

const orderList = ref<any[]>([]);

const { queryParams } = toRefs(data); 
const payType = [
    {label: '线上付款', value: 1},
    {label: '线下付款', value: 2},
    {label: '合同抵扣', value: 3},
    {label: '预付款抵扣', value: 4},
    {label: '月结', value: 5},
    {label: '抹零专用', value: 6},
];

const orderDetails = ref<any>({});
const addOrderVisible = ref(false);
const printOrderVisible = ref(false);
const editOrderVisible = ref(false);
const salerExamineVisible = ref(false);
const fianceExamineVisible = ref(false);
const checkProofVisible = ref(false);

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};

/** 点击新增 */
const handleClickAdd = () => {
    addOrderVisible.value = true;
};

const handleCloseAdd = () => {
    addOrderVisible.value = false;
};

/** 点击打印 */
const handleClickPrint = async (row: any) => {
    printOrderVisible.value = true;
};

const handleClosePrint = () => {
    printOrderVisible.value = false;
};

/** 点击编辑 */
const handleClickEdit = async (row: any) => {
    if(row.approvalStatus == '4' || row.approvalStatus == '2'){
        await proxy?.$modal.confirm('订单已经审核，无法被编辑');
    }else{
        editOrderVisible.value = true;
    }
};

const handleCloseEdit = () => {
    editOrderVisible.value = false;
};

const handleClickDelete = async (row: any) => {
    await proxy?.$modal.confirm('确认将此订单放入回收站么')
};

/** 点击发起销售审核 */
const handleInitiateSalerExamine = async (row: any) => {
    if(row.approvalStatus == '0'){
        await proxy?.$modal.confirm('确认发起销售审核？');
    }else{
        proxy?.$modal.msgError('该订单已发起审核');
    };
};

/** 点击销售审核 */
const handleSalerExamine = async (row: any) => {
    if(row.approvalStatus == '1'){
        orderDetails.value = row;
        salerExamineVisible.value = true;
    };
};

const handleCloseSalerExamine = () => {
    salerExamineVisible.value = false;
};

/** 点击财务审核 */
const handleClickFianceExamine = async (row: any) => {
    fianceExamineVisible.value = true;
};

const handleCloseFianceExamine = () => {
    fianceExamineVisible.value = false;
};

/** 点击撤销审核 */
const handleRevokeExamine = async (row: any) => {
    if(row.approvalStatus == '1'){
        await proxy?.$modal.confirm('撤销后的审核回归到未审核状态,确定撤销审核？')
    }else{
        proxy?.$modal.msgError('只有销售审核状态才可撤销！')
    };
};

/** 点击查看付款凭证 */
const handleClickCheckProof = async (row: any) => {
    checkProofVisible.value = true;
};

const handleCloseCheckProof = () => {
    checkProofVisible.value = false;
};

onMounted(()=>{
    getCustomerData();
    getUserListByRoleKey('business_role');
    const dataList = [];
    for(let i=1;i<=30;i++){
        const data = {
            id: i,
            createdByTime: '2024-11-07',
            orderNumber: `00000${i}`,
            createdByName: 'hnl',
            customerId: '1800730761616359425',
            company: 'A公司',
            price: 5000,
            sendMehod: '1',
            approvalStatus: '1',
            deliverStatus: '0',
            reconciliationStatus: '1',
            normal: '0'
        };
        dataList.push(data);
    };
    orderList.value = dataList;
});
</script>

<style scoped>

</style>